package com.jwt.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwt.commonutils.R;
import com.jwt.eduService.entity.EduTeacher;
import com.jwt.eduService.entity.vo.TeacherQuery;
import com.jwt.eduService.service.EduTeacherService;
import com.jwt.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin
public class EduTeacherController {

    //把service注入
    @Autowired
    private EduTeacherService teacherService;

    //每种操作有不同的风格  rest风格
    //查询get
    //添加post
    //修改put
    //删除delete

    /**
     * 查询所有讲师
     * @return
     */
    @ApiOperation(value = "查询所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service中的方法 查询所有
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("item",list);
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R deleteTeacher(@ApiParam(name = "id",value = "讲师ID",required = true)
                                     @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页讲师列表
     */
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("pageTeacher/{pageNum}/{pageSize}")
    public R PageListTeacher(@PathVariable long pageNum, @PathVariable long pageSize){
        Page<EduTeacher> page = new Page<>(pageNum,pageSize);
        teacherService.page(page,null);
        try {
            int i = 5/0;
        } catch (Exception e) {
            throw new GuliException(20001,"执行了自定义异常处理...");
        }
        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords();//每页数据list集合
        //第一种方法
//        Map map = new HashMap();
//        map.put("total",total);
//        map.put("item",records);
//        return R.ok().data(map);
        //第二中方法
        return R.ok().data("total",total).data("item",records);
    }

    /**
     * 条件查询带分页的方法
     */
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageTeacherCondition/{pageNum}/{pageSize}")
    public R pageTeacherCondition(@PathVariable long pageNum, @PathVariable long pageSize,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> page = new Page<>(pageNum,pageSize);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询  可能有也可能没有
        //判断条件值是否为空，如果不为空拼接条件
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String gmtCreate = teacherQuery.getGmtCreate();
        String gmtModified = teacherQuery.getGmtModified();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (level != null){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(gmtCreate)){
            wrapper.ge("gmt_create",gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtModified)){
            wrapper.le("gmt_modified",gmtModified);
        }
        //排序
        wrapper.orderByDesc("gmt_modified");
        //调用方法实现条件查询分页
        teacherService.page(page,wrapper);
        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords();//每页数据list集合
        return R.ok().data("total",total).data("item",records);
    }

    /**
     * 新增讲师
     * @return
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody(required = true)EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
     * 通过id查询讲师
     */
    @ApiOperation(value = "根据id查讲师")
    @GetMapping("getByIdTeacher/{id}")
    public R getByIdTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    /**
     * 修改讲师
     */
    @ApiOperation(value = "根据id修改讲师")
    @PostMapping("updateTeacher")
    public R getByIdTeacher(@RequestBody EduTeacher eduTeacher){
        boolean update = teacherService.updateById(eduTeacher);
        if (update){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

